//
//  CollectTableViewHeaderFooterView.m
//  XIWenLeJian
//
//  Created by lanou3g on 15/11/17.
//  Copyright © 2015年 蓝足. All rights reserved.
//

#import "CollectTableViewHeaderFooterView.h"
#import "Masonry.h"
#import "CollectModel.h"

@interface CollectTableViewHeaderFooterView ()

@property (nonatomic, strong)UIButton *headerBT;
@property (nonatomic, strong)UILabel *headerLabel;
@property (nonatomic, strong)UIImageView *loveImage;

@end

@implementation CollectTableViewHeaderFooterView

+ (instancetype)setHeaderView:(UITableView *)tableView
{
    static NSString *headerID = @"header";
    CollectTableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerID];
    if (!header) {
        header = [[CollectTableViewHeaderFooterView alloc]initWithReuseIdentifier:headerID];
    }
    return header;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.headerBT = [[UIButton alloc]init];
        self.headerBT.layer.borderWidth = 1.0;
        self.headerBT.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor grayColor]);
        self.headerBT.backgroundColor = [UIColor whiteColor];
        
        
        self.headerLabel = [[UILabel alloc]init];
        self.loveImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"iconfont-shoucang"]];
        [self.headerBT addSubview:self.loveImage];
        [self.headerBT addSubview:self.headerLabel];
        [self.headerBT addTarget:self action:@selector(show) forControlEvents:(UIControlEventTouchUpInside)];
        [self.contentView addSubview:self.headerBT];
    }
    return self;
}

- (void)setCollect:(CollectModel *)collectModel;
{
    _collectModel = collectModel;
    
    [self.headerBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.and.right.and.bottom.mas_equalTo(0);
    }];
    [self.loveImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(25, 25));
        make.right.mas_equalTo(- 10);
        make.top.mas_equalTo(10);
    }];
    [self.headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(50, 25));
        make.right.mas_equalTo(self.loveImage.mas_left).offset(-5);
        make.top.mas_equalTo(10);
    }];
    self.headerLabel.text = [NSString stringWithFormat:@"%ld", collectModel.content.count];
    self.headerLabel.textAlignment = NSTextAlignmentRight;
    
    [self.headerBT setTitle:collectModel.name forState:(UIControlStateNormal)];
    [self.headerBT setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
}

- (void)show
{
    self.collectModel.isShow = !self.collectModel.isShow;
    self.collectBlock();
}

@end
